<template>
  <div class="container">
    <van-nav-bar
      fixed
      title="注册账号"
      @click-left="$router.back()"
      left-arrow
    />

    <div class="logo-txt">
      欢迎注册
    </div>
    <van-form
      @submit="onSubmit"
      label-width="70px"
    >
      <van-field
        v-model="mobile"
        name="mobile"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        left-icon="manager"
        :rules="[{ required: true,pattern: /^[\d]{11}$/, message: '请填写11位手机号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        left-icon="lock"
        :rules="[{ required: true,   pattern: /^[\d\w\W]{6,10}$/,
				message: '请输入6-12位密码，只包含数字和字母', }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="repassword"
        label="确认密码"
        placeholder="确认密码"
        left-icon="lock"
        :rules="[{ required: true,  pattern: /^[\d\w\W]{6,10}$/,
				message: '请输入6-12位密码，只包含数字和字母', }]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
        >立即注册</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { register } from '@/api/user.js'

export default {
  data () {
    return {
      mobile: '',
      password: '',
    }
  },
  methods: {
    async onSubmit (values) {
      delete values.repassword
      console.log('submit', values)
      const res = await register(values)
      console.log(res);
      if (res.errno === 0) {
        this.$toast.success('注册成功')

        this.$router.push('/login')
      } else {
        this.$toast.fail(res.errmsg)
      }
    },
  },
}
</script>

<style scoped>
.logo-txt {
  text-align: center;
  font-size: 60px;
  margin: 60px auto;
  color: #1989fa;
}
</style>
